package cloud.shyidong.auth.controller;

import cloud.shyidong.auth.service.AuthService;
import cloud.shyidong.core.Result;
import cloud.shyidong.system.request.LoginRequest;
import cloud.shyidong.tenant.request.TenantLoginRequest;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Description TODO
 * @Author syd
 * @Date 2023/10/19
 */
@RestController
@Slf4j
@RequestMapping("/auth")
public class AuthController {
    
    private AuthService authService;
    
    @Autowired
    public void setAuthService(AuthService authService) {
        this.authService = authService;
    }
    
    /**
     * 管理员登录
     *
     * @param loginRequest
     * @return
     */
    @PostMapping("/admin/login")
    public Result adminLogin(@RequestBody @Valid LoginRequest loginRequest) {
        if (log.isDebugEnabled()) {
            log.debug("admin login param >>> {}", JSON.toJSONString(loginRequest));
        }
        return authService.adminLogin(loginRequest);
    }
    
    /**
     * 管理员查询登录信息
     *
     * @param token
     * @return
     */
    @GetMapping("/admin/get")
    public Result getAdminInfo(@RequestParam("token") String token) {
        if (log.isDebugEnabled()) {
            log.debug("admin token >>> {}", token);
        }
        return authService.getAdminInfo(token);
    }
    
    /**
     * 租户登录
     *
     * @param tenantLoginRequest
     * @return
     */
    @PostMapping("/tenant/login")
    public Result tenantLogin(@RequestBody @Valid TenantLoginRequest tenantLoginRequest) {
        if (log.isDebugEnabled()) {
            log.debug("tenant login param >>> {}", JSON.toJSONString(tenantLoginRequest));
        }
        return authService.tenantLogin(tenantLoginRequest);
    }
    
    /**
     * 租户查询登录信息
     *
     * @param token
     * @return
     */
    @GetMapping("/tenant/get")
    public Result getTenantLoginInfo(@RequestParam("token") String token) {
        if (log.isDebugEnabled()) {
            log.debug("tenant token >>> {}", token);
        }
        return authService.getTenantInfo(token);
    }
    
}
